package com.ld.control.business.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.user.mapper.MenuMapper;
import com.ld.control.business.user.mapper.UserAccountRoleMapper;
import com.ld.control.business.user.model.MenuModel;
import com.ld.control.business.user.model.UserAccountRoleModel;
import com.ld.control.business.user.service.MenuService;
import com.ld.control.business.user.vo.req.MenuReqVo;
import com.ld.control.business.user.vo.resp.MenuRespVo;
import com.ld.control.business.user.vo.resp.MenuTreeRespVo;
import com.ld.control.exception.BusinessException;
import com.ld.control.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ConcurrentModel;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-04-12
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuModel> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserAccountRoleMapper userAccountRoleMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Override
    public void add(MenuReqVo reqVo) {
        if (reqVo.getParentId() == null) {
            throw new BusinessException("父节点id未传！");
        }
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        MenuModel menuModel = new MenuModel();
        BeanUtils.copyProperties(reqVo, menuModel);
        menuModel.setCreateBy(currentUserAccountId);
        menuModel.setCreateTime(new Date());
        menuMapper.insert(menuModel);
    }

    @Override
    public void update(MenuReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        MenuModel menuModel = new MenuModel();
        BeanUtils.copyProperties(reqVo, menuModel);
        menuModel.setUpdateBy(currentUserAccountId);
        menuModel.setUpdateTime(new Date());
        menuMapper.updateById(menuModel);
    }

    @Override
    public void deleteById(Long id) {
        // 检查是否有下级菜单
        QueryWrapper<MenuModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(MenuModel.PARENT_ID, id);
        List<MenuModel> menuModelList = menuMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(menuModelList)) {
            throw new BusinessException("该菜单存在子菜单，不能删除！");
        }
        menuMapper.deleteById(id);
    }

    @Override
    public MenuRespVo getById(Long id) {
        MenuRespVo respVo = new MenuRespVo();
        MenuModel menuModel = menuMapper.selectById(id);
        BeanUtils.copyProperties(menuModel, respVo);
        return respVo;
    }

    @Override
    public List<MenuTreeRespVo> getList() {
        List<MenuTreeRespVo> menuTreeRespVoList = menuMapper.selectAllList();
        return getMenuTree(menuTreeRespVoList);
    }

    @Override
    public List<MenuTreeRespVo> getCurrentMenuTree() {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        QueryWrapper<UserAccountRoleModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UserAccountRoleModel.USER_ACCOUNT_ID, currentUserAccountId);
        List<UserAccountRoleModel> userAccountRoleModelList = userAccountRoleMapper.selectList(queryWrapper);
        List<Long> roleIdList = userAccountRoleModelList.stream().map(UserAccountRoleModel::getRoleId).toList();
        if (CollectionUtils.isEmpty(roleIdList)) {
            return new ArrayList<>();
        }
        List<MenuTreeRespVo> menuTreeRespVoList = menuMapper.selectListByRoleIdList(roleIdList, null);
        return getMenuTree(menuTreeRespVoList);
    }

    @Override
    public List<MenuTreeRespVo> getCurrentMenuList(Integer type) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        QueryWrapper<UserAccountRoleModel> queryWrapper = new QueryWrapper<>();
        if (currentUserAccountId != 1) {
            queryWrapper.eq(UserAccountRoleModel.USER_ACCOUNT_ID, currentUserAccountId);
        }
        List<UserAccountRoleModel> userAccountRoleModelList = userAccountRoleMapper.selectList(queryWrapper);
        List<Long> roleIdList = userAccountRoleModelList.stream().map(UserAccountRoleModel::getRoleId).toList();
        if (CollectionUtils.isEmpty(roleIdList)) {
            return new ArrayList<>();
        }
        return menuMapper.selectListByRoleIdList(roleIdList, type);
    }

    /**
     * 组装树形菜单
     * @param menuTreeReqList
     * @return
     */
    private List<MenuTreeRespVo> getMenuTree(List<MenuTreeRespVo> menuTreeReqList) {
        // 最终结果
        List<MenuTreeRespVo> resultMenus = new ArrayList<>();
        // 所有菜单集(含children)
        List<MenuTreeRespVo> loopMenus = new ArrayList<>();
        for (MenuTreeRespVo parent : menuTreeReqList) {
            // 第二层循环 获取每一个菜单的直接子菜单集
            List<MenuTreeRespVo> childrenList = new ArrayList<>();
            for (MenuTreeRespVo child : menuTreeReqList) {
                if (parent.getId().equals(child.getParentId())) {
                    childrenList.add(child);
                }
            }
            parent.setChildren(childrenList);
            loopMenus.add(parent);
        }

        // 找出最外层的菜单
        for (MenuTreeRespVo respVo : loopMenus) {
            if (respVo.getParentId() == 0) {
                resultMenus.add(respVo);
            }
        }
        return resultMenus;
    }

}
